# -*- coding: utf-8 -*-
import logging
import custom
from liberHook.BaseHook import BaseHook
from liberLibs import utils
from liberLibs import dcc
import liberConfig


def run_hook(library, hook, tool_directory, ext, start=1, end=1):
    mod = utils.load_hook(hook)
    if mod:
        try:
            hook_obj = mod.Hook(library, tool_directory, ext, start, end)
            hook_obj.main()
            if hook_obj.error_str:
                logging.error(hook_obj.error_str)
                return False
            return True
        except Exception as e:
            logging.error(str(e))
            return False
    return False


class Hook(object):
    def __init__(self, lib, folder):
        self.lib = lib
        self.folder = folder

    def files(self, ext):
        hook_obj = BaseHook(self.lib, self.folder, ext, 1, 1)
        return hook_obj.files

    @property
    def maya_renderer(self):
        from liberLibs.dccLibs import Maya
        renderer = Maya.get_current_renderer()
        if renderer not in custom.maya_support_renderer:
            renderer = custom.maya_renderer
        return renderer

    @property
    def run_env(self):
        return dcc.engine()

    def open_thumbnail(self):
        thumbnail_path = "%s/thumbnail.png" % self.folder
        utils.open_location(thumbnail_path)

    def maya_asset(self):
        if self.run_env == "maya":
            if not run_hook(self.lib, "maya_import", self.folder, ".ma"):
                run_hook(self.lib, "maya_import", self.folder, ".mb")
        elif self.run_env == "houdini":
            run_hook(self.lib, "houdini_abc_import", self.folder, ".abc")
        else:
            self.open_thumbnail()
            
    def maya_texture(self):
        if self.run_env == "maya":
            import maya.cmds as mc
            from liberLibs.dccLibs import Maya
            selected = Maya.selected()
            if not selected:
                run_hook(self.lib, "maya_texture_import", self.folder, ".files")
            else:
                selected_texture_nodes = False
                for sel in selected:
                    node_type = mc.nodeType(sel)
                    if node_type in liberConfig.TEXTURE_NODE_ATTR_DICT:
                        selected_texture_nodes = True
                        attr = "%s.%s" % (sel, liberConfig.TEXTURE_NODE_ATTR_DICT.get(node_type))
                        mc.setAttr(attr, self.files(".files")[0], type="string")
                if not selected_texture_nodes:
                    run_hook(self.lib, "maya_texture_import", self.folder, ".files")
        else:
            utils.open_location(self.files(".files")[0])

    def maya_shader(self):
        if self.run_env == "maya":
            import maya.cmds as mc
            from liberLibs.dccLibs import Maya
            selected = Maya.selected()
            if selected:
                if not run_hook(self.lib, "maya_shader_import_and_assign", self.folder, ".ma"):
                    run_hook(self.lib, "maya_shader_import_and_assign", self.folder, ".mb")
                mc.select(selected, r=1)
            else:
                if not run_hook(self.lib, "maya_import", self.folder, ".ma"):
                    run_hook(self.lib, "maya_import", self.folder, ".mb")
        else:
            self.open_thumbnail()

    def maya_light(self):
        if self.run_env == "maya":
            if not run_hook(self.lib, "maya_import", self.folder, ".ma"):
                run_hook(self.lib, "maya_import", self.folder, ".mb")
        else:
            self.open_thumbnail()

    def hdr(self):
        if self.run_env == "maya":
            if self.maya_renderer == "redshift":
                run_hook(self.lib, "maya_rs_dome_light", self.folder, ".files")
            else:
                run_hook(self.lib, "maya_ar_dome_light", self.folder, ".files")
        elif self.run_env == "houdini":
            run_hook(self.lib, "houdini_sky_light", self.folder, ".files")
        elif self.run_env == "clarisse":
            run_hook(self.lib, "clarisse_ibl_light", self.folder, ".files")
        else:
            self.open_thumbnail()

    def maya_vfx(self):
        if self.run_env == "maya":
            if not run_hook(self.lib, "maya_import", self.folder, ".ma"):
                run_hook(self.lib, "maya_import", self.folder, ".mb")
        else:
            self.open_thumbnail()

    def maya_xgen(self):
        if self.run_env == "maya":
            run_hook(self.lib, "maya_xgen_import", self.folder, ".xgen")
        else:
            self.open_thumbnail()

    def nuke_node(self):
        if self.run_env == "nuke":
            run_hook(self.lib, "nuke_node_import", self.folder, ".nk")
        else:
            self.open_thumbnail()

    def nuke_asset(self):
        if self.run_env == "nuke":
            run_hook(self.lib, "nuke_asset_import", self.folder, ".files")
        else:
            utils.open_location(self.files(".files")[0])

    def houdini_digital_asset(self):
        if self.run_env == "houdini":
            run_hook(self.lib, "houdini_import_hda", self.folder, ".hda")
        else:
            self.open_thumbnail()

    def maya_file(self):
        if self.run_env == "maya":
            if not run_hook(self.lib, "maya_open", self.folder, ".ma"):
                run_hook(self.lib, "maya_open", self.folder, ".mb")
        else:
            try:
                utils.open_location(self.files(".ma")[0])
            except:
                utils.open_location(self.files(".mb")[0])

    def nuke_file(self):
        if self.run_env == "nuke":
            run_hook(self.lib, "nuke_open", self.folder, ".nk")
        else:
            utils.open_location(self.files(".nk")[0])

    def houdini_file(self):
        if self.run_env == "houdini":
            run_hook(self.lib, "houdini_open", self.folder, ".hip")
        else:
            utils.open_location(self.files(".hip")[0])

    def clarisse_asset(self):
        if self.run_env == "clarisse":
            run_hook(self.lib, "clarisse_import_project", self.folder, ".project")

    def local_file(self):
        utils.open_location(self.files(".files")[0])

    def execute(self):
        if self.lib == "MayaAsset":
            self.maya_asset()
        elif self.lib == "MayaTexture":
            self.maya_texture()
        elif self.lib == "MayaShader":
            self.maya_shader()
        elif self.lib == "MayaLight":
            self.maya_light()
        elif self.lib == "HDR":
            self.hdr()
        elif self.lib == "MayaVfx":
            self.maya_vfx()
        elif self.lib == "Xgen":
            self.maya_xgen()
        elif self.lib == "NukeNode":
            self.nuke_node()
        elif self.lib == "NukeAsset":
            self.nuke_asset()
        elif self.lib == "HoudiniDigitalAsset":
            self.houdini_digital_asset()
        elif self.lib == "MayaFile":
            self.maya_file()
        elif self.lib == "NukeFile":
            self.nuke_file()
        elif self.lib == "HoudiniFile":
            self.houdini_file()
        elif self.lib == "ClarisseAsset":
            self.clarisse_asset()
        elif self.lib == "LocalFile":
            self.local_file()
